package org.example.dataHandle;

public class DataGroup<T extends Number> extends Data<T>{

    protected long timeSum = 0;
    protected long timeRange = 20000;

    protected long lptime = 0;

    public DataGroup(){
        super();
    }

    public void update(T input,long iptime) throws IllegalStateException{
        timeSum += iptime - lptime;
        lptime = iptime;
        Pair<T,Long> tempData = new Pair<>(input,timeSum);
        data.offer(tempData);
        while(timeSum - this.data.getFirst().getSecond() > timeRange){
            data.remove();
        }
    }


    public long getTimeRange(){
        return timeRange;
    }
    public void setTimeRange(int value){
        this.timeRange = value;
    }

    @Override
    public void clear() {
        super.clear();
        this.timeSum = 0;
        this.lptime = 0;
    }

    public boolean fillUntilFull(T input, long iptime){
        timeSum += iptime - lptime;
        lptime = iptime;
        Pair<T,Long> tempData = new Pair<>(input,timeSum);
        data.offer(tempData);
        return timeSum - this.data.getFirst().getSecond() > timeRange;
    }
}
